home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1000 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  64 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Extracting chars from shorts.
  5. Date: 10 Jan 1996 20:31:15 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan10133115@qcd.lanl.gov>
  8. References: <4d13i0$k3s@lastactionhero.rs.itd.umich.edu>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: dogcow@monet.ccs.itd.umich.edu's message of 10 Jan 1996 19:20:32 GMT
  13.  
  14. --text follows this line--
  15. In article <4d13i0$k3s@lastactionhero.rs.itd.umich.edu>
  16. dogcow@monet.ccs.itd.umich.edu (Tom Spindler) writes: 
  17. <snip>
  18.    I'm trying to extracts individual bytes from a double. The following code
  19.    doesn't work:
  20.  
  21.    typedef unsigned char zbyte;  /* unsigned 1 byte quantity */
  22.    typedef unsigned short zword; /* unsigned 2 byte quantity */
  23.  
  24.    typedef union zw_u_t {
  25.        zword zword;
  26.        struct {zbyte hi,lo;} zbyte;
  27.        } zwordun;
  28.  
  29.    #define lo(v)      ((zbyte) (((zwordun) v).zbyte.lo))
  30.    #define hi(v)      ((zbyte) (((zwordun) v).zbyte.hi))
  31.  
  32. Cast to an union type is not allowed. A cast to a pointer to union
  33. type is however fine.
  34.  
  35.    main() {
  36.    zword foon;
  37.    zbyte zfoo;
  38.  
  39.    zfoo = lo(foon);
  40.    }
  41.  
  42.    One compiler complains that it's an illegal cast, whereas gcc says that the
  43.    return value is an int.
  44.  
  45.    I could do something like ((char *)&v[0]), but then it would crash if I
  46.    tried to do something with lo(42).
  47.  
  48. Yes. In the general case, you have to assign the value to an union (or
  49. whatever) and then read off the fields.
  50.  
  51.    Any suggestions? (I already know what endian the code is supposed to run on.)
  52.  
  53. Cheers
  54. Tanmoy
  55.  
  56.  
  57. --
  58. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  59. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  60. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  61. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  62. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  63. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  64.